package com.bkgy.modular.calsidata.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bkgy.modular.alarm.entity.ZoneEntity;
import com.bkgy.modular.calsidata.entity.PlanprdList;
import com.bkgy.modular.calsidata.entity.Planwtpredict;
import com.bkgy.modular.calsidata.mapper.PlanprodListDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Component
//@Async
@Slf4j
public class YieldEstimation {
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private PlanprodListDao planProdListMapper;

    //@Scheduled(cron = "0 0 0,8,16 * * ?")
    public void saveSiData() {
        log.error("YieldEstimation+saveSiData");
        String data = restTemplate.getForObject("http://170.0.200.11:8090/restapii?name=C622_zonetrk", String.class);
        JSONObject jsonObject = JSONObject.parseObject(data);
        JSONArray zone_row = jsonObject.getJSONArray("zone_row");
        List<ZoneEntity> list2 = JSONObject.parseArray(zone_row.toString(), ZoneEntity.class);
        System.out.println(list2.get(0).getCoilid());
        List<PlanprdList> planprdList = planProdListMapper.findPlanprdList(list2.get(0).getCoilid());
        Double timesum = 0.0;
        Double wtsum = 0.0;
        Integer coilsum = 0;
        boolean flag = false;
        String hot_coil_id = "";
        Planwtpredict planwtpredict = new Planwtpredict();
        for (PlanprdList entity : planprdList) {
            if (timesum < 28800) {
                coilsum++;
                timesum += entity.getDuration();
                wtsum += entity.getIn_mat_wt();
                hot_coil_id = entity.getHot_coil_id();
                flag = false;
            } else {
                flag = true;
            }
        }
        if (flag) {
            planwtpredict.setRemark("根据当前已下发未生产的计划推算八小时,开始卷号" + list2.get(0).getCoilid() + ",结束卷号：" + hot_coil_id);
        } else {
            planwtpredict.setRemark("根据当前已下发未生产的计划推算未到8小时,开始卷号" + list2.get(0).getCoilid() + ",结束卷号：" + hot_coil_id);
        }

        planwtpredict.setLine("C622");
        planwtpredict.setSum_wt(wtsum / 1000);
        planwtpredict.setSum_coil(coilsum);
        Date date = new Date();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = dateFormat.format(date);
        int foo = Integer.parseInt(dateString.substring(11, 13));
        if (foo == 0) {
            planwtpredict.setShift(1);
        } else if (foo == 8) {
            planwtpredict.setShift(2);
        } else if (foo == 16) {
            planwtpredict.setShift(3);
        }
        planwtpredict.setDate1(dateString);

        planwtpredict.setCreatetime(new Date());
        planProdListMapper.insert(planwtpredict);
    }

}
